<!DOCTYPE html>
<html>
<head>
<title>加入Levi's以裤换酷行动</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
html,body{}
body,div{margin:0px;padding:0px;max-width:640px;margin:0px auto;}
.content{
position: relative;
width:100%;
margin:0px auto;
display:none;
background: transparent url('images/2.gif') no-repeat center top;background-size: cover;
}
.txt{position: relative;width:100%;margin:0px auto;background: transparent url('images/txt.png') no-repeat center top;background-size: cover;}
.header{
position: relative;
width:100%;
margin:0px auto;
display:none;
}
.htxt{position: relative;width:100%;margin:0px auto;}
.ling{
position: relative;
width:178px;
height:45px;
margin:0px auto;
}
.ling{position: relative;margin:0px auto;}
.ling a{width:100%;height:100%;display:block;}
</style>
</head>
<body><?php 
$card_id='pnPq7ju2H1No0Y1H5N3XCB9ZQNFY';
$code=NULL;

include_once("../core/config.php");
require_once "jssdk.php";
$jssdk = new JSSDK($appId, $secret);
$signPackage = $jssdk->GetSignPackage();
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
$sql="select id,code from p15 where status='work' limit 0 , 30 ";
$query = $mysqli->query($sql);
$codeInfo=array();
while ($list = $query->fetch_array()){
	$codeInfo[]=$list;
}
shuffle($codeInfo);
$code=$codeInfo[0]['code'];
$codeId=$codeInfo[0]['id'];
$mysqli->query("update p15 set status='not' where id='".$codeId."'  ");
//$content=file_get_contents('http://mei94.com/ka/sign.php?code='.$code.'&card_id='.$card_id);
$api_ticket=$secret;
$card_id=addslashes($card_id);
$timestamp=time();
$code=addslashes($code);
$openid='onPq7jnUiaW58_W9w-EsBiEnayhM';
$nickname='selimsong';
class Signature{
	function __construct(){
		$this->data = array();
	}
	function add_data($str){
		array_push($this->data, (string)$str);
	}
	function get_signature(){
		sort( $this->data, SORT_STRING );
		return sha1( implode( $this->data ) );
	}
};
$signature = new Signature();
$signature->add_data( $api_ticket );
$signature->add_data( $card_id );
$signature->add_data( $timestamp );
$signature->add_data( $code );
$sign=$signature->get_signature();
$content=json_encode(array('sign'=>$sign,'timestamp'=>$timestamp));

$kaInfo=json_decode($content);
$sign=$kaInfo->sign;
$timestamp=$kaInfo->timestamp;
?><div class="content">
      <div class="header"></div>
      <div class="txt"></div>
      <div class="htxt"></div>
      <div class="ling">
       <a class='jinru' data-val='<?php echo $card_id ?>' data-sign="<?php echo $sign ?>" data-time='<?php echo $timestamp ?>'>&nbsp;</a>
      </div>
       
</div>
<script src="../js/jquery-1.9.1.min.js"></script>
<script src="../js/velocity.min.js"></script>
<script src="../js/velocity.ui.min.js"></script>
<script type="text/javascript">
	var sWidth = parseInt((window.innerWidth > 0) ? window.innerWidth : screen.width);
	if(sWidth>640){sWidth=640;}
	var _musicStatus='close';
	var _rate=(sWidth/640);
	$('.content').height(_rate*1008);
	$('.header').height(_rate*200);
	$('.txt').height(_rate*309);
	$('.htxt').height(_rate*90);
	$('.content').show();
	$('.header').show();
	$('.jinru').velocity("transition.bounceDownIn", { duration: 1000 });
    $('.txt').velocity("transition.bounceDownIn", { duration: 1000 });
    $('.ling').width(_rate*460).height(_rate*150);
   var ua = navigator.userAgent;
    function get(){
    	$('.content').show();
		var reg = /MicroMessenger\/([\d\.]+)/i,
    		ret = ua.match(reg);
    	if (ret && ret[1]){
    		return ret[1];
    	}
    	return false;
    }
$(function(){
    
   function onBridgeReadyold() { 
        $('.jinru').click(function(e) { 
        $.get("code_update.php?id=<?php echo $codeId ?>", function(result){ });
        WeixinJSBridge.invoke('openCardDetail', {
        "card_id": $(this).attr('data-val'),
        "card_ext": "{\"code\":\"<?php echo $code ?>\",\"openid\":\"\",\"timestamp\":\""+$(this).attr('data-time')+"\",\"signature\":\""+$(this).attr('data-sign')+"\"}"
        },
        function(res) {}); });
    }
    
    function onBridgeReady() { 
        $('.jinru').click(function(e) {
        $.get("code_update.php?id=<?php echo $codeId ?>", function(result){ });
        WeixinJSBridge.invoke('batchAddCard', {
            "card_list": [{
                "card_id": $(this).attr('data-val'),
                "card_ext": "{\"code\":\"<?php echo $code ?>\",\"openid\":\"\",\"timestamp\":\""+$(this).attr('data-time')+"\",\"signature\":\""+$(this).attr('data-sign')+"\"}"
            }]
        },
        function(res) {}); });
    }
    
    if (typeof WeixinJSBridge == "undefined") {
        if(get()>='5.5'){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady);
        }else{
             document.addEventListener('WeixinJSBridgeReady', onBridgeReadyold);
        }
    }else {
        if(get()>='5.5'){
             onBridgeReady(); 
        }else{
          onBridgeReadyold();
      } 
    }

    function onBridgeReadyshare(){
     WeixinJSBridge.call('showOptionMenu');
    }

    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReadyshare, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', onBridgeReadyshare); 
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReadyshare);
        }
    }else{
        onBridgeReadyshare();
    }



   wx.config({
	    appId: '<?php echo $signPackage["appId"];?>',
	    timestamp: <?php echo $signPackage["timestamp"];?>,
	    nonceStr: '<?php echo $signPackage["nonceStr"];?>',
	    signature: '<?php echo $signPackage["signature"];?>',
	    jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'showMenuItems', 'hideMenuItems']
	  });
	  wx.ready(function () {
		    var _title='';
		    var _desc="加入Levi's以裤换酷行动";
		    var _imgUrl='http://wechat.levi.com.cn/kabao/logo.jpg';
		    var _link='http://wechat.levi.com.cn/kabao/5/?wechat_card_js=1';
			wx.onMenuShareTimeline({
			    title: _desc, 
			    link: _link, 
			    imgUrl: _imgUrl,
			    success: function () {   },
			    cancel: function () { }
			});
			wx.onMenuShareAppMessage({
			    title: _title, 
			    desc: _desc,
			    link: _link,
			    imgUrl: _imgUrl, 
			    type: '', 
			    dataUrl: '', 
			    success: function () {  },
			    cancel: function () {  }
			});
 
	     });


});
</script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</div>
</body>
</html>